<?php
namespace wsys;

use wsys\template\Template;

class Wsys extends \WeModuleSite
{

    protected $className;

    // 暂存数据对象
    private $assignData = [];

    // 其他模板引擎类
    private $T = null;

    protected function _initialize($module = false, $controller = false, $action = false)
    {
        global $_GPC;
        load()->model('site');

        $controller = $controller ?: $_GPC['do'];
        $action = $action ?: $_GPC['et'];

        define('MODULE_NAME', $module ?: \wsys\Config::get('default_module'));
        define('CONTROLLER_NAME', $controller);
        define('ACTION_NAME', $action ?: 'index');

        // 加载框架文件
        $this->loadExtend();

        // 加载附加功能
        $this->appendExtend();

        // 执行核心程序
        $this->run();
    }
    
    

    /**
     * 加载框架必备内容
     */
    protected function loadExtend()
    {
        include WSYS_COMMON_PATH . 'functions' . EXT;

        $app_function_path = APP_PATH . 'function' . DS;

        // 加载框架内所有自定义function.php，定位在wsys/function文件夹中
        if (is_dir($app_function_path)) {
            $files = scandir($app_function_path);

            if (!empty($files)) {
                $commonIndex = array_search('common.php', $files);
                if ($commonIndex !== false) {
                    // common.php拥有最高加载权
                    include $app_function_path . 'common.php';
                    unset($files[$commonIndex]);
                }
                foreach ($files as $file) {
                    if ($file == '.' || $file == '..') {
                        continue;
                    }
                    if (preg_match('/\.php$/is', $file)) {
                        include $app_function_path . $file;
                    }

                }

            }

        }
    }

    protected function appendExtend() 
    {
        if (TEMPLATE == 'THINK') {
            include LIB_PATH . 'template' . DS . 'Template' . EXT;
            $this->T = new Template();
        }
    }

    private function run()
    {
        global $_W;
        $inc = ucfirst(CONTROLLER_NAME);
        
        $class = "\\application\\" . MODULE_NAME . "\\controller\\" . $inc;
        $controller = new $class;
        
        $action = ACTION_NAME;
        if(!method_exists($controller, $action)) {
            throw new \Exception("$inc $action 方法不存在");
            return false;
        }
        $res = $controller->$action($_W, $_GET, $_POST);

        $code = $controller->getCode();
        $message = $controller->getMessage();
        $data = $controller->getData();

        // 请求返回
        if(is_bool($res)) {
            if($_W['isajax']) {
                $this->result($code, $message, $data);
            } else {
                message($message, $data['message_url'] ?: 'referer', $res ? 'success' : 'error');
            }
        } else if(is_string($res)) {
            $this->assign($controller->cache('wsys_data'));
            if(strpos($res, '/')) {
                $this->display($res);
            } else {
                $this->display(strtolower($inc) . '/' . $res);
            }
        }
    }

    // IS_DEV = true
    final public function doWebDev() { $this->_initialize(); }

    /**
     * 支付成功处理逻辑
     */
    final public function payResult($ret) 
    {
        $this->loadExtend();
        if ($ret['result'] == 'success') {
            logic('Notice');
            $noticeLogic = new \wsys\Logic\Notice;
            $result = $noticeLogic->pay($ret);
            if($result === false) {
                message($noticeLogic->getMessage());
            }
        }
        parent::payResult($ret);
    }

}
